package com.scaffold.authority.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.authority.entity.User;

@Repository
public class UserDAO extends BasicBizDAO<User,Long>{

	public List<User> pageQuery(Page page, String username, String password, String keyword) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if(username != null){
			criteria.add(Restrictions.eq("username", username));
		}
		if(password != null){
			criteria.add(Restrictions.eq("password", password));
		}
		if(keyword!=null && !keyword.isEmpty()){
			LogicalExpression expr = Restrictions.or(
					Restrictions.like("username", keyword, MatchMode.ANYWHERE),
					Restrictions.like("password", keyword, MatchMode.ANYWHERE));
			criteria.add(expr);
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

}
